package com.hex.ds.hdtp.core.inf.script.analyze.repository.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.common.page.converter.IPageAndMyBatisPlusPageConverter;
import com.hex.ds.hdtp.core.inf.script.analyze.mapper.IScriptAnalyzeTaskMapper;
import com.hex.ds.hdtp.core.inf.script.analyze.po.ScriptAnalyzeTaskPo;
import com.hex.ds.hdtp.core.inf.script.analyze.repository.IScriptAnalyzeTaskRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.inf.script.analyze.repository.impl
 * @ClassName ScriptAnalyzeTaskRepository
 * @Description 脚本分析任务-数据服务接口实现
 * @Author gj.xu
 * @Date 2023/12/8 15:49
 * @Version v2.0
 **/
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptAnalyzeTaskRepository implements IScriptAnalyzeTaskRepository {

    private final IScriptAnalyzeTaskMapper scriptAnalyzeTaskMapper;

    private final IPageAndMyBatisPlusPageConverter pageConverter;


    @Override
    public Page<ScriptAnalyzeTaskPo> queryListByPage(ScriptAnalyzeTaskPo scriptAnalyzeTaskPo, Page page) {
        final IPage tmpPageResult = scriptAnalyzeTaskMapper.queryListByPage(
                pageConverter.toMyBatisPlusPage(page)
                , scriptAnalyzeTaskPo
        );
        final List list = tmpPageResult.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageResult);
        return pageResult.setData(list);
    }

    @Override
    public void modifyTotal(String pkId, Integer total) {
        scriptAnalyzeTaskMapper.modifyTotal(pkId, total);
    }

    @Override
    public void modifySuccessCount(String pkId) {
        scriptAnalyzeTaskMapper.modifySuccessCount(pkId);
    }

    @Override
    public void modifyFailCount(String pkId) {
        scriptAnalyzeTaskMapper.modifyFailCount(pkId);
    }

    @Override
    public void modifyStatus(String pkId) {
        scriptAnalyzeTaskMapper.modifyStatus(pkId, DateUtil.now());
    }

    /**
     * @Method modifyStartTsById
     * @Param pkId
     * @Param status
     * @Return java.lang.Integer
     * @Description 更新启动任务时状态、成功失败数、开始时间
     * @Author gj.xu
     * @Date 2024/1/8 11:53
     * @Version V2.0
     */
    @Override
    public Integer modifyStartTsById(String pkId, String status) {
        return scriptAnalyzeTaskMapper.modifyStartTs(pkId, status, DateUtil.now());
    }

    @Override
    public ScriptAnalyzeTaskPo queryById(String pkId) {
        return scriptAnalyzeTaskMapper.selectById(pkId);
    }

    @Override
    public Integer add(ScriptAnalyzeTaskPo scriptAnalyzeTaskPo) {
        return scriptAnalyzeTaskMapper.insert(scriptAnalyzeTaskPo);
    }

    @Override
    public Integer modifyById(ScriptAnalyzeTaskPo scriptAnalyzeTaskPo) {
        return scriptAnalyzeTaskMapper.updateById(scriptAnalyzeTaskPo);
    }

    @Override
    public Integer removeById(String pkId) {
        return scriptAnalyzeTaskMapper.deleteById(pkId);
    }
}
